package net.noday.demo.aop;

import java.lang.reflect.Method;

import net.noday.demo.service.ITestService;
import net.noday.demo.service.TestService;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CGlibProxyFactory implements MethodInterceptor {

	private Object target;
	
	public Object createProxyIntance(Object target) {
		this.target = target;
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(this.target.getClass());//�̳�Ŀ���࣬�Է������и���
		enhancer.setCallback(this);
		return enhancer.create();
	}

	@Override
	public Object intercept(Object arg0, Method arg1, Object[] args,
			MethodProxy methodProxy) throws Throwable {
		ITestService ts = (TestService) this.target;
		Object result = null;
		if (ts.getUser() != null) {
			methodProxy.invoke(target, args);
		}
		return result;
	}
}
